package com.eric.flowabledemo.factory;

import com.eric.flowabledemo.constant.RmaStatusEnum;
import com.eric.flowabledemo.strategy.AbstractRmaProcessStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class RmaProcessStrategyFactory implements ApplicationContextAware {

    private static final Map<RmaStatusEnum, AbstractRmaProcessStrategy> PROCESS_STRATEGY_MAP = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, AbstractRmaProcessStrategy> strategyMap = applicationContext.getBeansOfType(AbstractRmaProcessStrategy.class);
        strategyMap.forEach((key, value) -> {
            PROCESS_STRATEGY_MAP.put(value.status(), value);
        });
    }

    public static AbstractRmaProcessStrategy getStrategy(RmaStatusEnum statusEnum){
        return PROCESS_STRATEGY_MAP.get(statusEnum);
    }
}
